#-*-coding:utf-8-*-
'''
Created on 2011-1-16

@author: zhaoyh
'''
import web
from config import render_mako
from addons.portal.company.model import Company,init,update
from db.meta import getsession
from utils.utility import AppStatus,uploadfile
view=render_mako(directories=['templates/portal/company'],input_encoding='utf-8',output_encoding='utf-8')
class edit():
    def GET(self,co_id=1):
        conn=getsession()
        if co_id==None or co_id==u'':co_id=1
        edit_co=conn.query(Company).first()
        if edit_co==None:
            init()
            return "<h1 class='contentTitle'>初始化成功,请重新打开！<h1>"
        else:
            print edit_co
            return view.company_edit(co=edit_co)
    def POST(self,co_id=1):
        if co_id==None or co_id==u'':co_id=1
        u=web.input()
        info=update(1,**u)
        if info:
            msg=AppStatus.ok()
        else:
            msg= AppStatus.failure("请检查")
        return msg
class upload():
    def GET(self,co_id):
        co_id=1
        return view.company_upload(co=co_id)
    def POST(self,co_id=1):
        if co_id==None or co_id==u'':co_id=1
        u=web.input(co_logo={})
        info=True
        if 'co_logo' in u: # to check if the file-object is created
            filepath=u.co_logo.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            uploadfile(filename,u.co_logo.file)
            info=update(co_id,co_logo=filename)
        if info:
            msg=AppStatus.ok_forward("company")
        else:
            msg= AppStatus.failure("请检查")
        return msg
if __name__ == "__main__":
    print '1'

        